﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace WSPDocGenerator
{
    class PackageReader
    {
        internal static string _folder = null;
        static IHelpGenerator _generator = null;

        internal static string Execute(string folder, IHelpGenerator generator)
        {
            _generator = generator; 
            
            _folder = string.Format("{0}{1}", folder, !folder.EndsWith("\\") ? "\\" : "");

            string manifestFile = string.Format("{0}Manifest.xml", _folder);

            // Generate documentation for WSP, manifest and features
            ProcessWSP(new DirectoryInfo(folder).Name);
            LoadManifest(manifestFile);
            EnumerateFeatureManifest(manifestFile);
            LoadFeatureManifest(manifestFile);

            // Save documentation for every WSP
            string output = _generator.Write();
            string helpFile = string.Format("{0}\\{1}.{2}",
                new DirectoryInfo(_folder).Parent.FullName, new DirectoryInfo(_folder).Name, _generator.Extension);
            File.Delete(helpFile);
            File.AppendAllText(helpFile, output);

            return output;
        }

        internal static void ProcessWSP(string folder)
        {
            _generator.WSP(folder);
        }

        internal static void LoadManifest(string manifestFile)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(manifestFile);

            _generator.Manifest(xmlDoc); 
        }

        internal static void EnumerateFeatureManifest(string manifestFile)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(manifestFile);

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsmgr.AddNamespace(string.Empty, "http://schemas.microsoft.com/sharepoint/");
            nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/sharepoint/");


            XmlNodeList nodes = xmlDoc.SelectNodes("//ns:Solution/ns:FeatureManifests/ns:FeatureManifest", nsmgr);
            foreach (XmlNode node in nodes)
            {
                string featureFile = string.Format("{0}{1}", _folder, node.Attributes["Location"].Value);

                xmlDoc = new XmlDocument();
                xmlDoc.Load(featureFile);
                _generator.EnumFeatures(xmlDoc, node.Attributes["Location"].Value);
            }

            _generator.RenderFeature();
        }

        internal static void LoadFeatureManifest(string manifestFile)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(manifestFile);

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsmgr.AddNamespace(string.Empty, "http://schemas.microsoft.com/sharepoint/");
            nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/sharepoint/");


            XmlNodeList nodes = xmlDoc.SelectNodes("//ns:Solution/ns:FeatureManifests/ns:FeatureManifest", nsmgr);
            foreach (XmlNode node in nodes)
            {
                string featureFile = string.Format("{0}{1}", _folder, node.Attributes["Location"].Value);
                
                xmlDoc = new XmlDocument();
                xmlDoc.Load(featureFile);

                _generator.Feature(xmlDoc, node.Attributes["Location"].Value);

                XmlNodeList elementNodes = xmlDoc.SelectNodes("//ns:Feature/ns:ElementManifests/ns:ElementManifest", nsmgr);
                foreach (XmlNode elementNode in elementNodes)
                {
                    FileInfo featureFileInfo = new FileInfo(featureFile);
                    string elementFile = string.Format("{0}\\{1}", 
                        featureFileInfo.Directory, 
                        elementNode.Attributes["Location"].Value);

                    XmlDocument xmlElementDoc = new XmlDocument();
                    xmlElementDoc.Load(elementFile);

                    _generator.Element(xmlElementDoc, elementNode.Attributes["Location"].Value);
                }
            }
        }

        internal static void LoadElementManifest(string featureFile)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(featureFile);

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsmgr.AddNamespace(string.Empty, "http://schemas.microsoft.com/sharepoint/");
            nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/sharepoint/");

            XmlNodeList nodes = xmlDoc.SelectNodes("//ns:Feature/ns:ElementManifests/ns:ElementManifest", nsmgr);
            foreach (XmlNode node in nodes)
            {
                string elementFile = string.Format("{0}\\{1}", Path.GetDirectoryName(featureFile), node.Attributes["Location"].Value);

                xmlDoc = new XmlDocument();
                xmlDoc.Load(elementFile);

                _generator.Element(xmlDoc, node.Attributes["Location"].Value);
            }
        }

    }
}
